package cn.zane.designPattern.createPatterns.singletonPatterns.design;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author: zane
 * @Date: 2025/01/05
 * @Description: CAS
 */
public class Singleton_06 {

    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<Singleton_06>();

    private static Singleton_06 instance;

    private Singleton_06() {
    }

    public static final Singleton_06 getInstance() {
        for (; ; ) {
            Singleton_06 instance = INSTANCE.get();
            if (null != instance) {
                return instance;
            }
            // CAS是一种无锁操作，是CPU指令集操作。具备原子操作。速度快。
            INSTANCE.compareAndSet(null, new Singleton_06());
            return INSTANCE.get();
        }
    }
}
